package com.ruoyi.biz.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.biz.pojo.dto.ActiveValidDTO;
import com.ruoyi.biz.pojo.dto.GenCodeDTO;
import com.ruoyi.biz.pojo.vo.R;
import com.ruoyi.biz.util.RespCode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.biz.domain.BizCode;
import com.ruoyi.biz.service.IBizCodeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 激活码Controller
 *
 * @author jason
 * @date 2025-10-31
 */
@Controller
@RequestMapping("/biz/code")
public class BizCodeController extends BaseController {
    private String prefix = "biz/code";

    @Autowired
    private IBizCodeService bizCodeService;

    @RequiresPermissions("biz:code:view")
    @GetMapping()
    public String code() {
        return prefix + "/code";
    }

    /**
     * 查询激活码列表
     */
    @RequiresPermissions("biz:code:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizCode bizCode) {
        startPage();
        List<BizCode> list = bizCodeService.selectBizCodeList(bizCode);
        return getDataTable(list);
    }

    /**
     * 导出激活码列表
     */
    @RequiresPermissions("biz:code:export")
    @Log(title = "激活码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizCode bizCode) {
        List<BizCode> list = bizCodeService.selectBizCodeList(bizCode);
        ExcelUtil<BizCode> util = new ExcelUtil<BizCode>(BizCode.class);
        return util.exportExcel(list, "激活码数据");
    }

    /**
     * 新增激活码
     */
    @RequiresPermissions("biz:code:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存激活码
     */
    @RequiresPermissions("biz:code:add")
    @Log(title = "激活码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizCode bizCode) {
        return toAjax(bizCodeService.insertBizCode(bizCode));
    }

    /**
     * 修改激活码
     */
    @RequiresPermissions("biz:code:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizCode bizCode = bizCodeService.selectBizCodeById(id);
        mmap.put("bizCode", bizCode);
        return prefix + "/edit";
    }

    /**
     * 修改保存激活码
     */
    @RequiresPermissions("biz:code:edit")
    @Log(title = "激活码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizCode bizCode) {
        return toAjax(bizCodeService.updateBizCode(bizCode));
    }

    /**
     * 删除激活码
     */
    @RequiresPermissions("biz:code:remove")
    @Log(title = "激活码", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizCodeService.deleteBizCodeByIds(ids));
    }

    /**
     * 生成激活码
     */
    @RequiresPermissions("biz:code:add")
    @GetMapping("/generate")
    public String generate() {
        return prefix + "/generate";
    }

    /**
     * 新增保存激活码
     */
    @RequiresPermissions("biz:code:add")
    @Log(title = "激活码生成", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    @ResponseBody
    public AjaxResult generate(GenCodeDTO param) {
        return AjaxResult.success(bizCodeService.generate(param));
    }

    /**
     * 激活
     */
    @Log(title = "激活码", businessType = BusinessType.UPDATE)
    @PostMapping("/active")
    @ResponseBody
    public R active(@RequestBody ActiveValidDTO param) {
        if (!StrUtil.isAllNotBlank(param.getClientId(), param.getCode())) {
            return R.fail(param, RespCode.PARAM_INVALID);
        }

        return bizCodeService.active(param);
    }

    /**
     * 状态
     */
    @Log(title = "激活码", businessType = BusinessType.UPDATE)
    @PostMapping("/status")
    @ResponseBody
    public R status(@RequestBody ActiveValidDTO param) {
        if (!StrUtil.isAllNotBlank(param.getClientId(), param.getCode())) {
            return R.fail(param, RespCode.PARAM_INVALID);
        }

        return bizCodeService.checkStatus(param);
    }
}
